﻿package Engine.RMI
{
    import Framework.Serialize.*;
    import Framework.Util.*;
    import flash.events.*;
    import flash.utils.*;

    public class RMIObjectBind extends Object
    {
        private var _session:Session;
        private var _rmiObject:RMIObject;
        private var _buffer:SerializeStream;
        private var _outgoingStatus:int;
        private var _messageId:int;
        private var _timer:Timer;
        private var _createTimer:Number;
        public static const EOutGoingWaitToSend:int = 0;
        public static const EOutGoingTimeOut:int = 1;
        public static const EOutGoingComplate:int = 2;

        public function RMIObjectBind()
        {
            this._outgoingStatus = EOutGoingWaitToSend;
            this._createTimer = getTimer();
            return;
        }// end function

        public function initTimer(param1:int) : void
        {
            if (param1 >= 0)
            {
                this._timer = new Timer(param1, 1);
                this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onComplete);
                this._timer.start();
            }
            return;
        }// end function

        public function resertTimer() : void
        {
            if (this._timer)
            {
                this._timer.reset();
                this._timer = null;
            }
            return;
        }// end function

        private function onComplete(event:TimerEvent) : void
        {
            var _loc_2:TimeoutException = null;
            this._session.removeBackObject(this.messageId);
            if (this._timer != null)
            {
                this._timer.reset();
            }
            this._outgoingStatus = EOutGoingTimeOut;
            if (this._rmiObject != null)
            {
                _loc_2 = new TimeoutException();
                if (this._session.communicator.prepareCommandHandler)
                {
                    this._rmiObject.usedTimer = getTimer() - this._createTimer;
                    this._session.communicator.prepareCommandHandler.prepareBackException(this._rmiObject, _loc_2);
                }
                this._rmiObject.cdeException(_loc_2);
            }
            return;
        }// end function

        public function __back(param1:Context, param2:SerializeStream) : void
        {
            var _loc_3:Exception = null;
            this._outgoingStatus = EOutGoingComplate;
            if (param1._dispachStatus.equals(ERMIDispatchStatus.DispatchOK))
            {
                if (this._session.communicator.prepareCommandHandler)
                {
                    this._rmiObject.usedTimer = getTimer() - this._createTimer;
                    this._session.communicator.prepareCommandHandler.prepareBackCommand(this._rmiObject);
                }
                this.rmiObject.__response(param2);
            }
            else if (param1._dispachStatus.equals(ERMIDispatchStatus.DispatchException))
            {
                _loc_3 = new Exception();
                param2.readException(_loc_3);
                if (this._session.communicator.prepareCommandHandler)
                {
                    this._rmiObject.usedTimer = getTimer() - this._createTimer;
                    this._session.communicator.prepareCommandHandler.prepareBackException(this._rmiObject, _loc_3);
                }
                this._rmiObject.cdeException(_loc_3);
            }
            return;
        }// end function

        public function __response(param1:ERMIDispatchStatus, param2:SerializeStream) : void
        {
            if (this._messageId == 0)
            {
                return;
            }
            var _loc_3:* = new SRMIReturn();
            _loc_3.messageId = this._messageId;
            _loc_3.dispatchStatus = param1;
            var _loc_4:* = new Context();
            _loc_4._session = this.session;
            _loc_4._connection = this.session.connection;
            Outgoing.dispatch(_loc_4, _loc_3, param2);
            return;
        }// end function

        public function __exception(param1:Exception) : void
        {
            if (this._messageId == 0)
            {
                return;
            }
            var _loc_2:* = new SRMIReturn();
            _loc_2.messageId = this._messageId;
            _loc_2.dispatchStatus = ERMIDispatchStatus.DispatchException;
            var _loc_3:* = new Context();
            _loc_3._session = this.session;
            _loc_3._connection = this.session.connection;
            var _loc_4:* = new SerializeStream();
            _loc_4.writeException(param1);
            Outgoing.dispatch(_loc_3, _loc_2, _loc_4);
            return;
        }// end function

        public function set session(param1:Session) : void
        {
            this._session = param1;
            return;
        }// end function

        public function get session() : Session
        {
            return this._session;
        }// end function

        public function set rmiObject(param1:RMIObject) : void
        {
            this._rmiObject = param1;
            return;
        }// end function

        public function get rmiObject() : RMIObject
        {
            return this._rmiObject;
        }// end function

        public function set buffer(param1:SerializeStream) : void
        {
            this._buffer = param1;
            return;
        }// end function

        public function get buffer() : SerializeStream
        {
            return this._buffer;
        }// end function

        public function set outgoingStatus(param1:int) : void
        {
            this._outgoingStatus = param1;
            return;
        }// end function

        public function get outgoingStatus() : int
        {
            return this._outgoingStatus;
        }// end function

        public function set messageId(param1:int) : void
        {
            this._messageId = param1;
            return;
        }// end function

        public function get messageId() : int
        {
            return this._messageId;
        }// end function

    }
}
